package pl.taskman.view.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.event.MouseInputListener;

import pl.taskman.helper.ColorHelper;

public class CustomButton extends JButton
{
	/**
	 * Generated serial ID
	 */
	private static final long			serialVersionUID	= 4135415330640190302L;
	public static boolean				mouseOver			= false;
	public static boolean				mousePressed		= false;
	private Color						color				= null;

	private CustomButtonMouseListener	cbml				= null;

	public CustomButton(Color color)
	{
		super();
		setBorderPainted(false);

		this.color = color;
		
		if (cbml == null) cbml = new CustomButtonMouseListener();

		addMouseListener(cbml);
		addMouseMotionListener(cbml);
	}

	@Override
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gradient = null;

		// FIXME Better hover effect
		if (mouseOver)
			gradient = new GradientPaint(getWidth() / 4, 0, ColorHelper.lighten(color, 20),
							getWidth() / 4, getHeight(), ColorHelper.darken(color, 40), false);
		
		// FIXME Better clicked effect
		if (mousePressed)
			gradient = new GradientPaint((getWidth() / 2), 0, ColorHelper.darken(color, 50),
							(getWidth() / 2), getHeight(), ColorHelper.darken(color, 100), false);
		
		if (!mouseOver && !mousePressed)
			gradient = new GradientPaint((getWidth() / 2), 0, color,
							(getWidth() / 2), (int) (getHeight() / 0.8),
							ColorHelper.darken(color, 100), false);

		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB);
		
		g2.setPaint(gradient);
		g2.fillRoundRect(1, 1, getWidth() - 1, getHeight() - 1, 25, 25);

		int fontSize = (int) Math.floor((getHeight() / 3)) + 2;

		g2.setFont(new Font("Dialog", Font.BOLD, fontSize));
		FontMetrics metrics = g2.getFontMetrics();
		Rectangle2D rect = metrics.getStringBounds(getText(), g2);

		double rectWidth = rect.getWidth();
		double rectHeight = rect.getHeight();

		int posX = (int) ((getWidth() / 2) - (rectWidth / 2));
		int poxY = (int) ((getHeight() / 2) + (rectHeight / 2) - 4);

		g2.setColor(Color.WHITE);
		g2.drawString(getText(), posX, poxY);
	}
}

class CustomButtonMouseListener implements MouseInputListener
{
	public CustomButtonMouseListener()
	{
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		CustomButton.mousePressed = true;
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		CustomButton.mousePressed = false;
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		CustomButton.mouseOver = true;
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		CustomButton.mouseOver = false;
	}

	@Override
	public void mouseDragged(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseMoved(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}
}
